<%@page import="java.util.Random"%>
<%@page import="java.awt.Font"%>
<%@page import="java.awt.Color"%>
<%@page import="java.awt.Graphics2D"%>
<%@page import="javax.imageio.ImageIO"%>
<%@page import="com.sun.prism.Image"%>
<%@page import="java.awt.image.BufferedImage"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    

<%@page isELIgnored="false"%><!--解除被禁用的el表达式 --> 
<%
	int width=120;
	int height=60;
	
	BufferedImage buffImg=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
	
	
	Graphics2D g2=buffImg.createGraphics();
	
	
	//设置颜色
	g2.setColor(Color.WHITE);
	g2.fillRect(0, 0, width, height);
	
	g2.setColor(Color.BLACK);
	g2.drawRect(0, 0, width-2, height-2);
	
	Font font=new Font("Time new Roman",Font.BOLD,20);
	g2.setFont(font);
	
	int c=0,b=0,g=0;

	StringBuffer sb=new StringBuffer();
	Random r=new Random();
	for(int i=0;i<4;i++){
		int a=r.nextInt(10);
		c=r.nextInt(150);
		g=r.nextInt(50);
		b=r.nextInt(250);
		g2.setColor(new Color(c,g,b));
	g2.drawString(a+"", 13*(i+1), 30);
	sb.append(a);
	}
	//使用会话session传承验证码
	session.setAttribute("loginCode", sb.toString());
	
	response.setHeader("Pragma", "no-cache");
	response.setHeader("Cache-Control", "no-cache");
	response.setDateHeader("Exprise", 0);
	response.setContentType("image/jpeg");
	
	ServletOutputStream sos=response.getOutputStream();
	ImageIO.write(buffImg, "jpeg", sos);
	
	//防止页面的执行错误
	sos.close();
	out.clear();
	out=pageContext.pushBody();
	
	

%>

